#!/usr/bin/env python

import wx

class TestPopup(wx.PopupWindow):
    """Adds a bit of text and mouse movement to the wx.PopupWindow"""
    def __init__(self, parent, style):
        wx.PopupWindow.__init__(self, parent, style)
        self.SetBackgroundColour("CADET BLUE")

        st = wx.StaticText(self, -1,
                          "This is a special kind of top level\n"
                          "window that can be used for\n"
                          "popup menus, combobox popups\n"
                          "and such.\n\n"
                          "Try positioning the demo near\n"
                          "the bottom of the screen and \n"
                          "hit the button again.\n\n"
                          "In this demo this window can\n"
                          "be dragged with the left button\n"
                          "and closed with the right."
                          ,
                          pos=(10,10))

        sz = st.GetBestSize()
        self.SetSize( (sz.width+20, sz.height+20) )

        self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
        self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
        self.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp)
        self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)

        st.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
        st.Bind(wx.EVT_MOTION, self.OnMouseMotion)
        st.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp)
        st.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)

        wx.CallAfter(self.Refresh)

    def OnMouseLeftDown(self, evt):
        self.Refresh()
        self.ldPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())
        self.wPos = self.ClientToScreen((0,0))
        self.CaptureMouse()

    def OnMouseMotion(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            dPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())
            nPos = (self.wPos.x + (dPos.x - self.ldPos.x),
                    self.wPos.y + (dPos.y - self.ldPos.y))
            self.Move(nPos)

    def OnMouseLeftUp(self, evt):
        self.ReleaseMouse()

    def OnRightUp(self, evt):
        self.Show(False)
        self.Destroy()
#-----------------------------------------------------------------------
class TestTransientPopup(wx.PopupTransientWindow):
    """Adds a bit of text and mouse movement to the wx.PopupWindow"""
    def __init__(self, parent, style):
        wx.PopupTransientWindow.__init__(self, parent, style)
        self.SetBackgroundColour("#FFB6C1")
        st = wx.StaticText(self, -1,
                          "wx.PopupTransientWindow is a\n"
                          "wx.PopupWindow which disappears\n"
                          "automatically when the user\n"
                          "clicks the mouse outside it or if it\n"
                          "(or its first child) loses focus in \n"
                          "any other way."
                          ,
                          pos=(10,10))
        sz = st.GetBestSize()
        self.SetSize( (sz.width+20, sz.height+20) )
        
        st.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp)

    def OnMouseLeftUp(self, evt):
        self.Show(False)
        self.Destroy()
        
#-----------------------------------------------------------------------------
class TestPanel(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, -1, style=0)

        self.search = wx.SearchCtrl(self, size=(200,-1),
                                style=wx.TE_PROCESS_ENTER )
        self.search.ShowCancelButton(1)
        self.search.ShowSearchButton(1)    
        #self.search.SetMenu(self.MakeMenu())

        # Setup the layout
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        #sizer.Add((15,15))
        sizer.Add(self.search, 0, wx.ALL)

        self.SetSizer(sizer)

        # Set event bindings
        self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, self.OnSearch, self.search)
        self.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN, self.OnCancel, self.search)
        self.Bind(wx.EVT_TEXT_ENTER, self.OnDoSearch, self.search)  

    def OnSearch(self, evt):
        win = TestTransientPopup(self, wx.SIMPLE_BORDER)
        #win = TestPopupWithListbox(self, wx.SIMPLE_BORDER, self.log)
        
        # Show the popup right below or above the button
        # depending on available screen space...
        btn = evt.GetEventObject()
        pos = btn.ClientToScreen( (0,0) )
        sz =  btn.GetSize()
        win.Position(pos, (0, sz[1]))
        
        win.Show(True) 
        
            
    def OnCancel(self, evt):
        ()

    def OnDoSearch(self, evt):
        win = TestPopup(self, wx.SIMPLE_BORDER)
        #win = TestPopupWithListbox(self, wx.SIMPLE_BORDER, self.log)
        
        # Show the popup right below or above the button
        # depending on available screen space...
        btn = evt.GetEventObject()
        pos = btn.ClientToScreen( (0,0) )
        sz =  btn.GetSize()
        win.Position(pos, (0, sz[1]))
        
        win.Show(True)        

    def MakeMenu(self):
        menu = wx.Menu()
        item = menu.Append(-1, "Recent Searches")
        item.Enable(False)
        for txt in [ "bobdos",
                     "hello world",
                     "search strings here",
                     "yum",
                     "catch their selections" ]:
            menu.Append(-1, txt)
        return menu 

#---------------------------------------------------------------------------
class MyFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, -1, "My Search Bar",
                            size = (210,50),
                            style = wx.CAPTION
                                   |wx.FRAME_TOOL_WINDOW
                                   |wx.STAY_ON_TOP
                                   |wx.SYSTEM_MENU
                                   |wx.CLOSE_BOX )

        panel = TestPanel(self, -1)

        self.Bind(wx.EVT_BUTTON, self.OnCloseWindow)
        
    def OnCloseWindow(self, event):
        self.Destroy()

#---------------------------------------------------------------------------
        
if __name__=='__main__':
        app = wx.PySimpleApp()
        frame = MyFrame(parent=None, id=-1)
        frame.Show()
        app.MainLoop()
        
#end of bailuo
